package controllers;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import models.*;
import notifiers.Mails;
import play.mvc.Controller;
import play.mvc.With;

import java.util.*;

import play.libs.Mail;
import models.*;
import notifiers.Mails;

import play.libs.Mail;
import play.mvc.Controller;
import play.mvc.With;

import java.util.Calendar;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.persistence.PersistenceException;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class Components extends Controller{

	public static void createComponent(String name,String desc, long projectId)
	{		
		Security.checkAccess(
			"Edit my project", projectId,
			Project.class.getName());

			Project p = Project.findById(projectId);
			List<Component> comps = p.components;
			boolean found = false;
			if(comps!=null)
			for(int i = 0; i<comps.size();i++)
				if(comps.get(i).name.equals(name))
				{
					found = true;
					break;
				}
			if(!found)
			{
			Component c = new Component(name,desc,p);
			p.components.add(c);
			p.save();
			c.save();
			new Notification(Long.parseLong(session.get("userId")), "New Component", name+" has been successfully created").save();
			new Log(Long.parseLong(session.get("userId")), (Long)c.getEntityId(), "Component", 
					"Component Created", projectId, "").save();
			}
			else flash.error("A component with the same name already exists");
			index(projectId);
	}
	
	public static void editComponent(String name,String desc, long compId,long projectId)
	{
		Security.checkAccess(
				"Edit my project", projectId,
				Project.class.getName());
		
		Project p = Project.findById(projectId);
		List<Component> comps = p.components;
		boolean found = false;
		if(comps!=null)
		for(int i = 0; i<comps.size();i++)
			if(comps.get(i).name.equals(name))
			{
				found = true;
				break;
			}
		if(!found)
		{
		Component c = Component.findById(compId);
		String oldName = c.name;
		c.name = name;
		c.description = desc;
		c.save();
		
		new Notification(Long.parseLong(session.get("userId")), "Component Edited", oldName+" has been successfully changed to " + name).save();

		new Log(Long.parseLong(session.get("userId")), (Long)c.getEntityId(), "Component", 
				"Component Edited", projectId, "").save();
		}
		else flash.error("A component with the same name already exists");
		index(projectId);

		
	}

	public static void deleteComponent(long compId,long projectId)
	{
		Security.checkAccess(
				"Edit my project", projectId,
				Project.class.getName());
		Component c = Component.findById(compId);
		c.delete();
		flash.success("Component deleted !");
		index(projectId);
	}

	public static List<Component> getProjectComponents(long projectId)
	{
		Project p  = Project.findById(projectId);
		return p.components;
	}

	public static void showComponent(long compId,long projectId)
	{
		Component comp = Component.findById(compId);
		Project project = Project.findById(projectId);
		List<UserStory> stories = comp.userStoriesAssigned;
		render(comp,project,stories);
	}

	public static void showCreateComponent(long projectId)
	{
		Project project = Project.findById(projectId);
		render(project);
				
	}
	public static void showEditComponent(long compId,long projectId)
	{
		Component comp = Component.findById(compId);
		Project project = Project.findById(projectId);
		render(project,comp);
		
	}
	
	public static void index(long projectId)
	{
		List<Component> projComp = getProjectComponents(projectId);
		for(int i = 0;i<projComp.size();i++)
			if(projComp.get(i).name.equals("Default component"))
				{
				projComp.remove(i);
				break;
				}
		Project project = Project.findById(projectId);
		render(projComp,project);
	}
}
